00001 /* 00002 * Copyright (c) 2013 Battelle Memorial Institute 00003 * Licensed under modified BSD License. A copy of this license can be found 00004 * in the LICENSE file in the top level directory of this distribution. 00005 */ 00006 // Emacs Mode Line: -*- Mode:c++;-*- 00007 // ------------------------------------------------------------- 00008 /** 00009 * @file distributed.hpp 00010 * @author William A. Perkins 00011 * @date 2013-11-08 11:45:15 d3g096 00012 * 00013 * @brief 00014 * 00015 * 00016 */ 00017 00018 // ------------------------------------------------------------- 00019 00020 #ifndef _distributed_hpp_ 00021 #define _distributed_hpp_ 00022 00023 #include "gridpack/parallel/parallel.hpp" 00024 00025 namespace gridpack { 00026 namespace parallel { 00027 00028 // ------------------------------------------------------------- 00029 // class DistributedInterface 00030 // ------------------------------------------------------------- 00031 /// Abstract class describing a parallel thing 00032 /** 00033 * A parallel thing either contains or has access to a Communicator. 00034 * A parallel thing needs access to this communicator, especially to 00035 * determine the number processors and the local processor rank. 00036 * 00037 */ 00038 class DistributedInterface { 00039 protected: 00040 00041 public: 00042 00043 /// Default constructor. 00044 DistributedInterface(void); 00045 00046 /// Copy constructor 00047 DistributedInterface(const DistributedInterface& old); 00048 00049 /// Destructor 00050 virtual ~DistributedInterface(void); 00051 00052 /// Get the communicator 00053 virtual const Communicator& communicator(void) const = 0; 00054 00055 /// Get this processor's rank 00056 int processor_rank(void) const 00057 { 00058 return this->communicator().rank(); 00059 } 00060 00061 /// Get the size of the parallel environment 00062 int processor_size(void) const 00063 { 00064 return this->communicator().size(); 00065 } 00066 }; 00067 00068 00069 00070 // ------------------------------------------------------------- 00071 // class Distributed 00072 // ------------------------------------------------------------- 00073 /// Serves as a base class for parallel things 00074 /** 00075 * Subclasses of this class are things that exist simultaneously on 00076 * multiple processes. Instantiation and destruction occurs 00077 * simultaneously on all processes. The communicator is available 00078 * directly to subclasses. 00079 * 00080 */ 00081 class Distributed 00082 : public DistributedInterface 00083 { 00084 protected: 00085 00086 /// The parallel environmnet 00087 Communicator communicator_; 00088 00089 public: 00090 00091 /// Default constructor. 00092 explicit Distributed(const Communicator& comm); 00093 00094 /// Copy constructor 00095 Distributed(const Distributed& old); 00096 00097 /// Destructor 00098 virtual ~Distributed(void); 00099 00100 /// Get the communicator 00101 const Communicator& communicator(void) const; 00102 }; 00103 00104 // ------------------------------------------------------------- 00105 // class WrappedDistributed 00106 // ------------------------------------------------------------- 00107 /// A distributed object that's just a wrapper around another distributed object 00108 /** 00109 * This serves as a subclass to a class that wraps a Distributed class 00110 * instance. In that case, it's desireable to have the wrapper class 00111 * act like a Distributed instance, but not duplicate the information 00112 * in the wrapped class. 00113 * 00114 */ 00115 class WrappedDistributed 00116 : public DistributedInterface 00117 { 00118 protected: 00119 00120 /// The actual distributed object 00121 Distributed *p_distributed; 00122 00123 /// Set the wrapped object 00124 void p_setDistributed(Distributed *d) { 00125 p_distributed = d; 00126 } 00127 00128 /// Default constructor. 00129 WrappedDistributed(Distributed *d = NULL); 00130 00131 public: 00132 00133 /// Protected copy constructor to avoid unwanted copies. 00134 WrappedDistributed(const WrappedDistributed& old); 00135 00136 /// Destructor 00137 ~WrappedDistributed(void); 00138 00139 /// Get the communicator 00140 const Communicator& communicator(void) const; 00141 }; 00142 00143 00144 00145 } // namespace parallel 00146 } // namespace gridpack 00147 00148 #endif